package com.lm.common.core.util;

import com.lm.common.core.constant.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> success() {
        return restResult(CommonConstants.SUCCESS, null, null);
    }

    public static <T> R<T> success(T data) {
        return restResult(CommonConstants.SUCCESS, null, data);
    }

    public static <T> R<T> success(String msg, T data) {
        return restResult(CommonConstants.SUCCESS, msg, data);
    }

    public static <T> R<T> error() {
        return restResult(CommonConstants.FAIL, null, null);
    }

    public static <T> R<T> error(String msg) {
        return restResult(CommonConstants.FAIL, msg, null);
    }

    public static <T> R<T> error(T data) {
        return restResult(CommonConstants.FAIL, null, data);
    }

    public static <T> R<T> error(String msg, T data) {
        return restResult(CommonConstants.FAIL, msg, data);
    }

    private static <T> R<T> restResult(int code, String msg, T data) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }

}
